package demoMod.stsconsole.commands;

import demoMod.stsconsole.ConsoleEntry;
import demoMod.stsconsole.cards.AbstractCard;
import demoMod.stsconsole.dungeons.AbstractDungeon;
import demoMod.stsconsole.enums.ErrType;
import demoMod.stsconsole.helpers.LocalizedStrings;
import demoMod.stsconsole.interfaces.PreSmithSubscriber;
import demoMod.stsconsole.rooms.AbstractRoom;

import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Scanner;

public class Smith extends AbstractCommand implements PreSmithSubscriber {
    private boolean canSmith = true;

    public Smith() {
        ConsoleEntry.subscribers.add(this);
    }

    @Override
    public void execute(String[] args, Scanner sc) {
        if (args.length != 0) {
            this.errMsg(ErrType.WRONG_ARGUMENT);
            ConsoleEntry.removeLater.add(this);
            return;
        }
        if (AbstractDungeon.currDungeon.currMapNode.room.phase == AbstractRoom.RoomPhase.COMPLETED) {
            this.errMsg(ErrType.COMMAND_NOT_AVAILABLE);
            ConsoleEntry.removeLater.add(this);
            return;
        }
        ConsoleEntry.triggerSubscribers(PreSmithSubscriber.class,
                "onSmith",
                new Class[]{boolean.class},
                new Object[]{canSmith},
                (method, params, subscriber) -> {
                    if ((boolean) params[0]) {
                        try {
                            params[0] = method.invoke(subscriber, params);
                            canSmith = (boolean) params[0];
                        } catch (IllegalAccessException | InvocationTargetException e) {
                            e.printStackTrace();
                        }
                    }
                }
        );
        if (this.canSmith) {
            List<String> cmdStrings = LocalizedStrings.getCommandStrings("Smith");
            List<AbstractCard> cards = AbstractDungeon.player.getUpgradableCards();
            int i = 0;
            for (AbstractCard card : cards) {
                System.out.println(i + "\t" + card.name);
                i++;
            }
            System.out.print(cmdStrings.get(0));
            try {
                int index = Integer.parseInt(sc.nextLine());
                if (index < 0 || index >= i) {
                    this.errMsg(ErrType.WRONG_ARGUMENT);
                    ConsoleEntry.removeLater.add(this);
                    return;
                }
                AbstractCard cardToUpgrade = cards.get(index);
                AbstractCard tmpUpgrade = cardToUpgrade.makeStatEquivalentCopy();
                tmpUpgrade.onRemove();
                tmpUpgrade.upgrade();
                System.out.println(cmdStrings.get(1)); //升级预览
                System.out.println(cmdStrings.get(2)); //升级前
                System.out.println(cardToUpgrade.name); //名称
                System.out.print(cmdStrings.get(3));
                if (cardToUpgrade.cost == -1) {
                    System.out.println("X");
                } else if (!cardToUpgrade.canUse) {
                    System.out.println("-");
                } else {
                    System.out.println(cardToUpgrade.cost);
                } //费用
                System.out.println(cardToUpgrade.getUpdatedDescriptions(null)); //描述
                System.out.println(cmdStrings.get(4)); //升级后
                System.out.println(tmpUpgrade.name); //名称
                System.out.print(cmdStrings.get(3));
                if (tmpUpgrade.cost == -1) {
                    System.out.println("X");
                } else if (!tmpUpgrade.canUse) {
                    System.out.println("-");
                } else {
                    System.out.println(tmpUpgrade.cost);
                } //费用
                System.out.println(tmpUpgrade.getUpdatedDescriptions(null)); //描述
                System.out.print(cmdStrings.get(5)); //是否确认升级
                if (sc.nextLine().equalsIgnoreCase("y")) {
                    System.out.println(cmdStrings.get(6) + cardToUpgrade.name); //已升级
                    cardToUpgrade.upgrade();
                    AbstractDungeon.currDungeon.currMapNode.room.phase = AbstractRoom.RoomPhase.COMPLETED;
                }
            } catch (NumberFormatException e) {
                this.errMsg(ErrType.WRONG_ARGUMENT);
            } finally {
                ConsoleEntry.removeLater.add(this);
            }
        } else {
            this.errMsg(ErrType.COMMAND_NOT_AVAILABLE);
        }
        ConsoleEntry.removeLater.add(this);
    }

    @Override
    public boolean requiredPrompt(String currentPrompt) {
        return currentPrompt.contains("Campfire");
    }

    @Override
    public boolean onSmith(boolean canSmith) {
        return AbstractDungeon.player.getUpgradableCards().size() > 0;
    }
}
